package org.csu.myPetStore.web.servlet;

import org.csu.myPetStore.domain.Account;
import org.csu.myPetStore.domain.Cart;
import org.csu.myPetStore.domain.CartItem;
import org.csu.myPetStore.service.CartService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Iterator;



public class UpdateCartQuantitiesServlet extends HttpServlet {

    private static final String VIEW_CART = "/WEB-INF/jsp/cart/Cart.jsp";
    private static final String ERROR = "/WEB-INF/jsp/common/Error.jsp";
    private static final String LOGIN = "/WEB-INF/jsp/account/SignonForm.jsp";

    private Cart cart;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        cart = (Cart) session.getAttribute("cart");

        Account account = (Account) session.getAttribute("account");

        if(account == null)
            request.getRequestDispatcher(LOGIN).forward(request,response);

        else{
            CartService service = new CartService();

            Iterator<CartItem> cartItems = cart.getAllCartItems();
            while (cartItems.hasNext()) {
                CartItem cartItem = cartItems.next();
                String itemId = cartItem.getItem().getItemId();
                try {
                    int quantity = Integer.parseInt(request.getParameter(itemId));
                    cart.setQuantityByItemId(itemId, quantity);
                    if (quantity < 1) {
                        service.deleteCartItemFromUserByItemId(itemId,account.getUsername());
                        cartItems.remove();
                    }else {
                        service.updateQuantityByItemid(quantity,itemId,account.getUsername());
                    }
                } catch (Exception e) {
                    session.setAttribute("message", "The Quantities of Item must be Integer!");
                    request.getRequestDispatcher(ERROR).forward(request, response);
                }
            }
            request.getRequestDispatcher(VIEW_CART).forward(request, response);
        }

    }

}
